<?php
class Debug {
	public $config = array (//初始化debug信息
		'preload' => array (
			'log' 
		),
		'components' => array (
			'db' => array (
				'enableProfiling' => true,
				'enableParamLogging' => true 
			),
			'log' => array (
				'class' => 'CLogRouter',
				'routes' => array (
					array (
						'class' => 'CProfileLogRoute',
						'levels' => 'trace, error, warning, profile, info',
						'categories' => 'system.db.*' 
					) 
				) 
			) 
		) 
	);

	public function __construct() {
		Yii::app ()->configure ( $this->config );
	}

	public function startTime() {
		return YII_BEGIN_TIME;
	}

	public function endTime() {
		return microtime ( true );
	}

	public function memoryUsage() {
		return (memory_get_usage () / 1024);
	}

	public function showRunTime() {
		echo '<hr>';
		echo 'Processed in <small>' . ($this->endTime () - $this->startTime ()) . '</small> second(s). ';
		echo 'Memory use <small>' . $this->memoryUsage () . '</small> Kb.';
	}

	public function showIncludeFiles() {
		$included_files = get_included_files ();
		$num = 0;
		$html = '';
		foreach ( $included_files as $fileName ) {
			$num ++;
			$html .= "{$num}. {$fileName}<br>";
		}
		
		echo '<div style="width:960px; margin:0 auto; text-align:left;">';
		echo 'Include ' . $num . ' files. <br>';
		echo $html;
		echo '</div>';
	}

	public function show() {
		$request = Yii::app ()->request;
		if (( boolean ) $request->getParam ( 'ajax', $request->isAjaxRequest ))
			return;
		echo '<div style="width:100%; text-align:center;">';
		$this->showRunTime ();
		$this->showIncludeFiles ();
		echo '</div>';
	}
}
?>